local Msg = require("core.Msg")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local EquipLogic = require("bag.EquipLogic")
local BagLogic = require("bag.BagLogic")
local EquipExcel = require("excel.Equip")
local StrengthConfig = require("excel.Strength")
local RefineDefine = require("refine.RefineDefine")
local Grid = require("bag.Grid")
local ShopLogic = require("shop.ShopLogic")
local Log = require("common.Log")
local RefineHandle = require("refine.HandlerL")
local TaskCallbacks = require("task.TaskCallbacks")
local TaskDefine = require("task.Define")
local StrenthAllConfig = StrengthConfig.strengthAll
local RoleAttr = require("role.RoleAttr")
local ItemDefine = require("bag.ItemDefine")
local OfficialLogic = require("official.OfficialLogic")

function open(human)
  local sendMsg = Msg.gc.GC_EQUIP_OPEN
  sendMsg.type = 1
  sendMsg.status[0] = human.db.equipLen
  for i=0, human.db.equipLen - 1 do
    local grid = human.db.equip[i]
    sendMsg.status[i + 1] = 1    
    if grid.id ~= 0 then
      local equip = grid.data.equip
      local strengthenID = EquipExcel.equip[grid.id].strengthenID
      local strengConfig = StrengthConfig[strengthenID][equip.strengthLv + 1]
      if equip.strengthLv < EquipExcel.equip[human.db.equip[i].id].strengthenLv
          and BagLogic.getItemCount(human, strengConfig.needItem[1]) >= strengConfig.needItem[2] 
          and ObjHuman.getYinliang(human) >= strengConfig.needYinliang then
        sendMsg.status[i + 1] = 0
      end
    end
  end
  local cache = {}
  sendMsg.qhInfo[0] = 0
  for k,v in pairs(StrengthConfig.runes) do
    sendMsg.qhInfo[0] = sendMsg.qhInfo[0] + 1
    Grid.create(cache, k, 1)
    Grid.makeItem(cache, sendMsg.qhInfo[sendMsg.qhInfo[0]].succRune, 0, human)
    sendMsg.qhInfo[sendMsg.qhInfo[0]].succRate = v.addSuccRate
  end
  Msg.send(sendMsg, human.fd)
end

function totalAttrs(total, addAttrs)
  for k,v in pairs(addAttrs) do
    local addNew = false
    for i=1,#total do
      if total[i].key == v[1] then
        total[i].value = v[2]
        addNew = true
        break
      end
      if not addNew then
        total[i+1].key = v[1]
        total[i+1].value = v[2]
        total[0] = total[0] + 1
      end
    end
  end
end

function chooseEquip(human, index)
  local sendMsg = Msg.gc.GC_CHOOSE_EQUIP
  local equip = human.db.equip[index].data.equip
  local strengthenID = EquipExcel.equip[human.db.equip[index].id].strengthenID  
  local strengConfig = StrengthConfig[strengthenID][equip.strengthLv + 1]
  sendMsg.jipin[0] = 0
  sendMsg.isMax = 2
  sendMsg.nextEquip[0] = 0
  sendMsg.stageNextLv = 0
  if equip.strengthLv >= EquipExcel.equip[human.db.equip[index].id].strengthenLv then
    strengConfig = nil
    sendMsg.isMax = 1
  end
  sendMsg.type = 1
  sendMsg.needItem[0] = 0
  local cache = {}
  if strengConfig then
    local addAttrs = strengConfig.addAttrs
    sendMsg.showRand = strengConfig.showRand
    sendMsg.needYinliang = strengConfig.needYinliang
    sendMsg.nextAddAttr[0] = #addAttrs
    for i=1, #addAttrs do
      sendMsg.nextAddAttr[i].key = addAttrs[i][1]
      sendMsg.nextAddAttr[i].value = addAttrs[i][2]    
    end
    sendMsg.isMax = 0
    sendMsg.needItem[0] = 1
    Grid.create(cache, strengConfig.needItem[1], strengConfig.needItem[2])
    Grid.makeItem(cache, sendMsg.needItem[1], 0, human)   
  else
    sendMsg.showRand = 0
    sendMsg.needZhangong = 0
    sendMsg.needYinliang = 0
    sendMsg.nextAddAttr[0] = 0  
    sendMsg.needItem[0] = 0 
  end
  Msg.send(sendMsg, human.fd)
end

function Strength(human, index, succRune, isAutoBuy)
  local grid = human.db.equip[index]
  local equip = grid.data.equip
  local strengthenID = EquipExcel.equip[grid.id].strengthenID
  local strengConfig = StrengthConfig[strengthenID][equip.strengthLv + 1]
  
  if succRune ~= 0 and not StrengthConfig.runes[succRune] then
    return 
  end  
  if equip.strengthLv >= EquipExcel.equip[human.db.equip[index].id].strengthenLv then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_QIANGHUA_LV_ENOUGH)
  end

  if ObjHuman.getYinliang(human) < strengConfig.needYinliang then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_YINLIANG_NOT_ENOUGH)
  end
  
  if succRune ~= 0 and BagLogic.getItemCount(human, succRune) < 1 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_QIANGHUA_RUNE_NOT_ENOUGH)
  end
  if BagLogic.getItemCount(human, strengConfig.needItem[1]) < strengConfig.needItem[2] then
    if strengConfig.needItem[1] ~= ItemDefine.ITEM_ID_STRENGTH_STONE_1 then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_QIANGHUA_ITEM_NOT_ENOUGH_2)
    elseif isAutoBuy ~= 1 then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_QIANGHUA_ITEM_NOT_ENOUGH)
    else
      local needBuy = {}
      needBuy[1] = {}
      needBuy[1][1] = strengConfig.needItem[1]
      needBuy[1][2] = strengConfig.needItem[2]
      if not ShopLogic.batchBuy(human, needBuy) then
        return
      end
    end    
  end  
  if succRune ~= 0 then
    BagLogic.delItemsById(human, succRune, 1, "qianghua")
    succRune = StrengthConfig.runes[succRune].addSuccRate * 100
  end
  BagLogic.delItemsById(human, strengConfig.needItem[1], strengConfig.needItem[2], "qianghua")
  ObjHuman.decYinliang(human, strengConfig.needYinliang, "qianghua")
  local getRand = math.random(1, 10000)
  local rand = strengConfig.successRand + succRune
  if strengConfig.showRand + succRune >= 10000 then
    rand = 10000
  end
--  local rand = (succRune ~= 0 and StrengthConfig.runes[succRune]) and strengConfig.showRand + StrengthConfig.runes[succRune].addSuccRate * 100 or strengConfig.successRand
  if getRand > rand then
    chooseEquip(human, index)
    RefineDefine.sendStatus(human, RefineDefine.REFINE_QIANGHUA_FAIL)
    Log.write(Log.LOGID_OSS_REFINE_QH, human._id,human.account,human.name,human.db.lv,grid.id,EquipExcel.equip[grid.id].pos,equip.strengthLv,equip.strengthLv,strengConfig.needYinliang,succRune,strengConfig.needItem[1],strengConfig.needItem[2],0)
    return
  end
  equip.strengthLv = equip.strengthLv + 1
  EquipLogic.GC_EQUIP_CHANGE(human,index)
  chooseEquip(human, index)
  open(human)
  RefineDefine.sendStatus(human, RefineDefine.REFINE_QIANGHUA_SUCCESS)
  ObjHuman.doCalc(human)
  RefineHandle.CG_OPEN_REFINE_WINDOW(human)
  OfficialLogic.checkOneCondition(human, "strengthLv")
  Log.write(Log.LOGID_OSS_REFINE_QH, human._id,human.account,human.name,human.db.lv,grid.id,EquipExcel.equip[grid.id].pos,equip.strengthLv - 1,equip.strengthLv,strengConfig.needYinliang,succRune,strengConfig.needItem[1],strengConfig.needItem[2],1)
  TaskCallbacks.taskRefineCB(human, TaskDefine.TASK_REFINE_STENGTHEN, EquipExcel.equip[grid.id].pos)
end
--检查是否可强化
function checkShowTips(human, info)
  local cache = {}
  for i=0,human.db.equipLen - 1 do
    local grid = human.db.equip[i]
    if grid ~= nil and grid.id ~= 0 then
      local equip = grid.data.equip
      local strengthenID = EquipExcel.equip[grid.id].strengthenID
      local strengConfig = StrengthConfig[strengthenID][equip.strengthLv + 1]
      if equip.strengthLv < EquipExcel.equip[human.db.equip[i].id].strengthenLv 
          and BagLogic.getItemCount(human, strengConfig.needItem[1]) >= strengConfig.needItem[2] 
          and ObjHuman.getYinliang(human) >= strengConfig.needYinliang then
        if info then
          info.equip[0] = 1
          Grid.create(cache, grid.id, 1)
          Grid.makeItem(cache, info.equip[1], 0, human)
        end  
        return 1
      end
    end
  end
  return 0
end
--发送全身强化增加的属性
function sendAllAttr(human)
  local sendMsg = Msg.gc.GC_STRENGTH_ALL_ADD_ATTR
  sendMsg.info[0] = #StrenthAllConfig
  for i=1, #StrenthAllConfig do
    sendMsg.info[i].strengthLv = StrenthAllConfig[i].strengthLv
    sendMsg.info[i].attr[0] = #StrenthAllConfig[i].addAttr
    for j=1, #StrenthAllConfig[i].addAttr do
      sendMsg.info[i].attr[j].key = StrenthAllConfig[i].addAttr[j][1]
      sendMsg.info[i].attr[j].value = StrenthAllConfig[i].addAttr[j][2]
    end
    sendMsg.info[i].power = RoleAttr.calPowerProto(sendMsg.info[i].attr) 
  end
  Msg.send(sendMsg, human.fd)
end
